package com.np.controller;

import com.np.WebSettings;
import com.np.model.OrderSheet;
import com.np.model.Product;
import com.np.model.User;
import com.np.util.HibernateUtil;
import com.sun.deploy.net.HttpResponse;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.hibernate.*;
import org.hibernate.jdbc.ReturningWork;
import org.hibernate.jdbc.Work;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.query.*;
import org.hibernate.query.Query;
import org.hibernate.stat.SessionStatistics;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HttpServletBean;

import javax.jws.soap.SOAPBinding;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.metamodel.*;
import javax.persistence.metamodel.Metamodel;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/18.
 */
@Controller
public class AdminController {

    private static Logger log = (Logger) LogManager.getLogger(UserControllr.class.getName());

    @RequestMapping("/admin")
    public String admin(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        if(user == null){
            return "login";
        }
        model.addAttribute("user",user);
        return "admin/admin";
    }

    @RequestMapping("/addproduct")
    public String addproduct(){

        return "admin/addproduct";
    }


    @RequestMapping(value = "/saveProduct")
    public String addProduct(Product p,MultipartFile path)
    {

        Product product = new Product();
        product = p;
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
        SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy/MM/dd/HH:mm");
        product.setTime(formatter2.format(currentTime));

        if (!path.isEmpty()) {
            try {
                byte[] bytes = path.getBytes();

                String  imgPath = WebSettings.IMAGE_URL+formatter.format(currentTime)+".jpg";
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(
                                new File(imgPath)));
                stream.write(bytes);
                stream.close();
                product.setImgPath("../img/"+formatter.format(currentTime)+".jpg");
            } catch (Exception e) {

            }
        } else {

        }


        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(product);

        session.getTransaction().commit();
        return "admin/addproduct";

    }

    @RequestMapping("/productlist")
    public String productlist(Model model){

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Product";
        Query query = session.createQuery(hql);

        List<Product> productList = query.list();
        model.addAttribute("productList",productList);

        return "admin/productlist";
    }

    @RequestMapping("/orderlist")
    public String orderlist(Model model){

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from OrderSheet ";
        Query query = session.createQuery(hql);

        List<OrderSheet> orderSheets = query.list();
        model.addAttribute("orderSheets",orderSheets);

        return "admin/orderlist";
    }

    @RequestMapping("/addOrder")
    public String addOrder(OrderSheet orderSheet){
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(orderSheet);
        session.getTransaction().commit();
        return "admin/addOrder";
    }

    @RequestMapping("/productupdate")
    public String productupdate(Model model){

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Product";
        Query query = session.createQuery(hql);

        List<Product> productList = query.list();
        model.addAttribute("productList",productList);

        return "admin/productupdate";
    }

    @RequestMapping("hh")
    public String hh(MultipartFile file, String a) throws IOException {

        if(!file.isEmpty())
        {
            byte[] s =  file.getBytes();
            FileOutputStream out = new FileOutputStream(new File("D:\\NetworkProduct\\image\\"+file.getOriginalFilename()));

            out.write(s);
        }
        return "";
    }


    @RequestMapping("/deleteProduct")
    public void deleteProduct(int id) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        Product p = s.load(Product.class,id);
        s.close();

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(p);
        session.getTransaction().commit();

        //return "redirect:/productupdate";
    }


    @RequestMapping("/xiugaiproduct")
    public String updateProduct(int id,Model model)
    {
        System.out.print("###################################-----"+id);
        Session session = HibernateUtil.getSessionFactory().openSession();
        Product product = session.load(Product.class,id);
        model.addAttribute("product",product);
        return "admin/xiugaiproduct";
    }

    @RequestMapping(value = "/updateProduct_xiugai",method = RequestMethod.POST)
    public String updateProduct_xiugai(Product product,Model model)
    {
        Session s = HibernateUtil.getSessionFactory().openSession();


        Product p = s.load(Product.class,product.getId());
        product.setTime(p.getTime());
        product.setImgPath(p.getImgPath());


        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        session.update(product);
        session.getTransaction().commit();
        return "redirect:/productupdate";
    }

}
